package org.example.misc.model;

import org.springframework.http.HttpStatus;

/**
 * 返回实体
 * businessCode 正常 默认 000000
 *
 * @param <T>
 */
public class Result<T> {

    private static final String RIGHT_BUSINESS_CODE = "000000";
    private static final String SUCCEED_MESSAGE = "request is success";

    private String httpCode;

    private String serverCode;

    private String message;

    private T data;

    public Result() {
    }

    public Result(T data) {
        this.httpCode = String.valueOf(HttpStatus.OK.value());
        this.serverCode = RIGHT_BUSINESS_CODE;
        this.message = SUCCEED_MESSAGE;
        this.data = data;
    }

    public Result<T> setHttpCode(HttpStatus status) {
        this.httpCode = String.valueOf(status.value());
        return this;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getServerCode() {
        return serverCode;
    }

    public void setServerCode(String serverCode) {
        this.serverCode = serverCode;
    }

    public String getHttpCode() {
        return httpCode;
    }

    public void setHttpCode(String httpCode) {
        this.httpCode = httpCode;
    }

    public static <T> Result<T> ok(T object) {
        return new Result<T>(object);
    }

    public Boolean isSucceed() {
        return String.valueOf(HttpStatus.OK.value()).equals(this.httpCode) && RIGHT_BUSINESS_CODE.equals(this.serverCode);
    }

    public Boolean isFailed() {
        return !isSucceed();
    }

}
